<?php
namespace app\api\controller;
use app\model\CommentModel;
use think\facade\Request;
use think\facade\Validate;

class comment{

    //列表
    public function comment_list(){
        $articleid = Request::param('article_id');
        $comment = CommentModel::where('article_id','=',$articleid)->select();
        $resule = $comment->toArray();
        $arr = [];
        foreach($resule as $row){
            $arr[] = [
                'comment_id' => $row['comment_id'],
                'comment_content' => $row['comment_content'],
                'email' => $row['email'],
                'nickname' => $row['nickname'],
                'add_time' => $row['add_time'   ],
                'random' => $row['random']
            ];
        }
        $date = [
            'status' => 0,
            'message' => '',
            'date' => $arr
        ];
        return json($date);
    }
    //增加评论
    public function add(){

        $comment = Request::param();
        $validate = Validate::rule([
//            'comment_id' => 'require|between:1,'.PHP_INT_MAX,
            'comment_content|内容' => 'require|min:2|max:500',
            'email|邮箱' => 'require|min:3|max:100',
            'nickname|昵称' => 'require|min:3|max:200',
            'article_id|文章编号' => 'require|between:0 ,' .PHP_INT_MAX
        ]);
        $check = $validate->check($comment);
        if(!$check){
            $date = [
                'status' => 10002,
                'message' => $validate->getError(),
                'date' => ''
            ];
            return json($date);
        }

        $array = ['1.jpg','2.jpg','3.jpg','4.jpg','5.jpg','6.jpg'];
        $mt =mt_rand(0,5);

        $date = $comment;
        $date['add_time'] = time();
        $date['update_time']  = time();

        $date['random'] = $array[$mt];
        $result = CommentModel::create($date);
        $date = [
            'status' => 0,
            'message' => '',
            'date' => $result->toArray()
        ];
        return json($date);
    }
}